using System;
using System.Linq;
using System.Text;
using Atom.Util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace WebCClient.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class Ids4Controller : ControllerBase
    {

        [AllowAnonymous]
        [HttpGet(nameof(IndexToken))]
        public IActionResult IndexToken()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("{");
            foreach (var key in Request.Query.Keys)
            {
                sb.AppendLine($"\"{key}\":\"{Request.Query[key].ToString()}\",");
            }
            sb.AppendLine("\"友情提示:\":\"请手动copy 地址栏# 后面的 参数值\"");
            sb.AppendLine("}");
            var txt = sb.ToString();
            return new ContentResult { Content = txt, ContentType = "application/json" };

        }

        [Authorize]
        //[Authorize(AuthenticationSchemes = "Bearer")]
        [HttpGet(nameof(Index))]
        public IActionResult Index()
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("{");
            foreach (var key in Request.Query.Keys)
            {
                sb.AppendLine($"\"{key}\":\"{Request.Query[key].ToString()}\",");
            }
            sb.AppendLine("\"site:\":\"WebCClient\""); //主要目的: 为了让json格式 是正确的
            StringBuilder sb_IdToken = new StringBuilder();

            //下面是Id_Token
            string id_token = base.HttpContext.Request.Cookies["id_token"];
            if (!string.IsNullOrEmpty(id_token))
            {
                var token_parts = id_token.Split('.');
                var header = Encoding.UTF8.GetString(Base64Url.Decode(token_parts[0]));
                var claims = Encoding.UTF8.GetString(Base64Url.Decode(token_parts[1]));
                var sign = Encoding.UTF8.GetString(Base64Url.Decode(token_parts[2])); //乱码

                sb_IdToken.AppendLine($"\"{"id_token"}\":\"{id_token}\",");
                sb_IdToken.AppendLine($"\"{"header"}\":\"{header}\",");
                sb_IdToken.AppendLine($"\"{"claims"}\":\"{claims}\",");
                sb_IdToken.AppendLine($"\"{"sign"}\":\"{sign}\",");
                RemoveLastChar(sb_IdToken);
                sb.AppendLine("\"Id_Token:\":{" + sb_IdToken + "},");
            }

            //下面是Access_Token
            StringBuilder sb_Access_Token = new StringBuilder();
            foreach (var item in base.HttpContext.User.Identities.First().Claims)
            {
                sb_Access_Token.AppendLine($"\"{item.Type}\":\"{item.Value}\",");
            }
            RemoveLastChar(sb_Access_Token);
            var Access_Token_Content = sb_Access_Token.ToString();

            sb.AppendLine("\"Access_Token:\":{" + Access_Token_Content + "}");

            sb.AppendLine("}");
            var txt = sb.ToString();
            return new ContentResult { Content = txt, ContentType = "application/json" };
        }

        public static StringBuilder RemoveLastChar(StringBuilder value)
        {
            return value == null || value.Length <= 0 ? value : value.Remove(value.Length - 1, 1);
        }

    }
}
